package com.hongkzh.www.adv.service;

import com.hongkzh.www.conmon.BigNumberFormat;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author martins
 */
public class EnterpriseAdvService extends BaseService {


    public List<Record> getEnterpriseCategoryList() {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseCategoryList");
        List<Record> records = Db.find(sqlPara);
        return records;
    }

    public Page<Record> getEnterpriseCategoryAdvList(String cid, int pageNumber, String sortId, String sortValue) {
        if("create_date".equals(sortId)) {sortId=" a.create_date ";}
        else if("rank".equals(sortId)){ sortId=" a.img_rank ";}
        Kv cond=Kv.by("cid",cid).set("order", " order by "+ sortId+" "+sortValue);
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseCategoryAdvList",cond);
        Page<Record> records=Db.paginate(pageNumber,3,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);

        }
        return records;
    }

    public List<Record> getEnterpriseHotAdvTypeList() {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseHotAdvTypeList");
        List<Record> list= Db.find(sqlPara);
        int currentHour,beginDate,endDate;
        for (Record r:list) {
            r.set("typeId",r.get("id"));
            r.remove("id");
            currentHour=r.getInt("currentHour");
            beginDate=r.getInt("beginDate");
            endDate=r.getInt("endDate");
            r.set("sortDate", currentHour-beginDate);
           if(currentHour>=beginDate&&currentHour<endDate){
               r.set("sortDate",0);
           }
           else{
               if(currentHour-beginDate>0) {
                   r.set("sortDate", currentHour-beginDate-24);
               }
           }
        }
        Collections.sort(list,new Comparator<Record>() {
            //升序排序
            @Override
            public int compare(Record o1,
                               Record o2) {
                return o2.getInt("sortDate").compareTo(o1.getInt("sortDate"));
            }

        });

        return list;
    }

    public Page<Record> getEnterpriseHotAdvList(String id,int p) {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseHotAdvList", id);
        Page<Record> records =Db.paginate(p,6,sqlPara);
       /* if (records == null) {
            records = Lists.newArrayList();
        }*/
        for (Record r : records.getList()) {

            doFilePath(r);
        }
        return records;
    }
    @Before(Tx.class)
    public String collection(String loginUid, String sid, String state) {
        if(StaticPublic.NO.equals(state)){
            Db.update(Db.getSql("user.deleteCollection"),loginUid,sid);
        }
        else if(StaticPublic.YES.equals(state)){
            String id=IdGen.uuid();
            Db.update(Db.getSql("user.collectionAdd"),id,sid,loginUid);
        }
        int counts=Db.findFirst(Db.getSqlPara("user.collectionList",sid)).getInt("counts");
        return BigNumberFormat.dataFormat(counts) ;
    }
    @Before(Tx.class)
    public String praise(String loginUid, String sid, String state) {
        if(StaticPublic.NO.equals(state)){
            Db.update(Db.getSql("user.deletePraise"),loginUid,sid);
            Db.update(Db.getSqlPara("user.updateDataPlusPraise",sid));

        }
        else if(StaticPublic.YES.equals(state)){
            String id=IdGen.uuid();
            Db.update(Db.getSql("user.praiseAdd"),id,sid,loginUid);
            Db.update(Db.getSqlPara("user.updateDataAddPraise",sid,id));
        }
        int counts=Db.findFirst(Db.getSqlPara("user.praiseList",sid)).getInt("counts");
        return BigNumberFormat.dataFormat(counts) ;
    }
}
